﻿using System;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using AEM.Data.Enum;
using AEM.Utils;
using Microsoft.Win32;
using Actions = AEM.Data.BLL.Actions;

namespace AEM
{
    /// <summary>
    /// LiftStateInfo.xaml 的交互逻辑
    /// </summary>
    public partial class LiftStatus : Window
    {
        public LiftStatus()
        {
            this.InitializeComponent();
            this.Loaded += new RoutedEventHandler(LiftStatus_Loaded);
            this.TitleBar.MouseLeftButtonDown += new MouseButtonEventHandler(LiftStatus_MouseLeftButtonDown);
            this.statusList.SelectionChanged += new SelectionChangedEventHandler(statusList_SelectionChanged);

            this.statusImport.Click += new RoutedEventHandler(statusImport_Click);
            this.statusAdd.Click += new RoutedEventHandler(statusAdd_Click);
            this.statusEdit.Click += new RoutedEventHandler(statusEdit_Click);
            this.statusDelete.Click += new RoutedEventHandler(statusDelete_Click);
            this.statusNum.TextChanged += new TextChangedEventHandler(statusNum_TextChanged);
        }

        void statusNum_TextChanged(object sender, TextChangedEventArgs e)
        {
            errmsg.Text = "";
            if (!string.IsNullOrEmpty(this.statusNum.Text.Trim()) && !this.statusNum.Text.Trim().isNumeric())
            {
                this.statusNum.Text = this.statusNum.Text.Substring(0, statusNum.Text.Length - 1);
                errmsg.Text = string.Format(this.TryFindResource("MustBeNumber") as string, this.TryFindResource("StatusNum") as string);
            }
        }

        void LiftStatus_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void sys_button_min_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void sys_button_close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        void LiftStatus_Loaded(object sender, RoutedEventArgs e)
        {
            this.statusList.ItemsSource = Data.DAL.LiftStatus.List("");
            //statusType.ItemsSource = StringHelper.EnumToList<StatusType>();
            //statusType.SelectedIndex = 0;
        }

        void statusList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Data.Model.LiftStatus row = (sender as ListView).SelectedItem as Data.Model.LiftStatus;
            if (row == null) { return; }
            statusNum.Text = row.LiftStatusNum.ToString();
            statusName.Text = row.LiftStatusName.ToString();
            statusType.SelectedIndex = row.LiftStatusType - 1;

        }

        void statusImport_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = this.TryFindResource("ImportTitle") as string;
            dlg.Filter = this.TryFindResource("ImportFilter") as string;
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                string filename = dlg.FileName;
                StreamReader sr = new StreamReader(filename, Encoding.Default);
                int lenok = 0, lenfail = 0;
                string errstr = "";
                while (!sr.EndOfStream)
                {
                    string tmp = sr.ReadLine();
                    if (tmp.Length <= 4) { errstr += tmp + "；"; lenfail++; continue; }
                    string numstr = tmp.Substring(0, 4);
                    string name = tmp.Substring(4);
                    if (!numstr.isNumeric()) { errstr += tmp + "；"; lenfail++; continue; }
                    int num = numstr.toNumeric();
                    int type = numstr.Substring(0, 1).toNumeric();
                    Data.Model.LiftStatus row = new Data.Model.LiftStatus();
                    row.LiftStatusNum = num;
                    row.LiftStatusName = name;
                    row.LiftStatusType = type;
                    Actions.StatusInfo.AddstatusResult ret = Actions.StatusInfo.Addstatusinfo(row);
                    switch (ret)
                    {
                        case Actions.StatusInfo.AddstatusResult.OK:
                            lenok++;
                            break;
                        default:
                            errstr += row.LiftStatusNum + row.LiftStatusName + "；";
                            lenfail++;
                            break;
                    }
                }
                errmsg.Text = string.Format(this.TryFindResource("ImportMsg") as string, lenok, lenfail, errstr);
                this.statusList.ItemsSource = Data.DAL.LiftStatus.List("");
                statusList.Items.Refresh();
                statusNum.Text = "";
                statusName.Text = "";
                statusType.SelectedIndex = 0;
            }

        }

        void statusAdd_Click(object sender, RoutedEventArgs e)
        {
            errmsg.Text = "";
            if (string.IsNullOrEmpty(statusNum.Text.Trim())) { errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("StatusNum") as string); return; }
            if (string.IsNullOrEmpty(statusName.Text.Trim())) { errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("StatusName") as string); return; }

            Data.Model.LiftStatus row = new Data.Model.LiftStatus();
            row.LiftStatusNum = Convert.ToInt32(statusNum.Text.Trim());
            row.LiftStatusName = statusName.Text.Trim();
            row.LiftStatusType = statusType.SelectedIndex + 1;
            Actions.StatusInfo.AddstatusResult ret = Actions.StatusInfo.Addstatusinfo(row);
            switch (ret)
            {
                case Actions.StatusInfo.AddstatusResult.DataIsNull:
                    errmsg.Text = string.Format(this.TryFindResource("IsNull") as string, this.TryFindResource("StatusInfo") as string);
                    break;
                case Actions.StatusInfo.AddstatusResult.StatusIsExist:
                    errmsg.Text = string.Format(this.TryFindResource("AlreadyExists") as string, this.TryFindResource("StatusInfo") as string);
                    break;
                case Actions.StatusInfo.AddstatusResult.StatusNumIsExist:
                    errmsg.Text = string.Format(this.TryFindResource("AlreadyExists") as string, this.TryFindResource("StatusNum") as string);
                    break;
                case Actions.StatusInfo.AddstatusResult.StatusNameIsExist:
                    errmsg.Text = string.Format(this.TryFindResource("AlreadyExists") as string, this.TryFindResource("StatusName") as string);
                    break;
                case Actions.StatusInfo.AddstatusResult.OK:
                    errmsg.Text = this.TryFindResource("successfuladd") as string;
                    this.statusList.ItemsSource = Data.DAL.LiftStatus.List("");
                    statusList.Items.Refresh();
                    statusNum.Text = "";
                    statusName.Text = "";
                    statusType.SelectedIndex = 0;
                    break;
            }
        }

        void statusEdit_Click(object sender, RoutedEventArgs e)
        {
            errmsg.Text = "";
            if (null == this.statusList.SelectedItem) { errmsg.Text = this.TryFindResource("SelectOne") as string; return; }
            if (string.IsNullOrEmpty(statusNum.Text.Trim())) { errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("StatusNum") as string); return; }
            if (string.IsNullOrEmpty(statusName.Text.Trim())) { errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("StatusName") as string); return; }

            Data.Model.LiftStatus row = (statusList.SelectedItem as Data.Model.LiftStatus);
            row.LiftStatusNum = Convert.ToInt32(statusNum.Text.Trim());
            row.LiftStatusName = statusName.Text.Trim();
            row.LiftStatusType = statusType.SelectedIndex + 1;
            Actions.StatusInfo.ModifiystatusResult ret = Actions.StatusInfo.Modifiystatus(row);
            switch (ret)
            {
                case Actions.StatusInfo.ModifiystatusResult.StatusIsNotExist:
                    errmsg.Text = string.Format(this.TryFindResource("IsNotExist") as string, this.TryFindResource("StatusInfo") as string);
                    break;
                case Actions.StatusInfo.ModifiystatusResult.StatusIsNull:
                    errmsg.Text = string.Format(this.TryFindResource("IsNull") as string, this.TryFindResource("StatusInfo") as string);
                    break;
                case Actions.StatusInfo.ModifiystatusResult.StatusNumIsExist:
                    errmsg.Text = string.Format(this.TryFindResource("AlreadyExists") as string, this.TryFindResource("StatusNum") as string);
                    break;
                case Actions.StatusInfo.ModifiystatusResult.StatusNameIsExist:
                    errmsg.Text = string.Format(this.TryFindResource("AlreadyExists") as string, this.TryFindResource("StatusName") as string);
                    break;
                case Actions.StatusInfo.ModifiystatusResult.OK:
                    errmsg.Text = this.TryFindResource("successfulupdate") as string;
                    this.statusList.ItemsSource = Data.DAL.LiftStatus.List("");
                    statusList.Items.Refresh();
                    statusNum.Text = "";
                    statusName.Text = "";
                    statusType.SelectedIndex = 0;
                    break;
            }
        }

        void statusDelete_Click(object sender, RoutedEventArgs e)
        {
            Data.Model.LiftStatus row = this.statusList.SelectedItem as Data.Model.LiftStatus;
            if (null == row) { errmsg.Text = this.TryFindResource("SelectOne") as string; return; }
            if (MessageBoxResult.Yes == MessageBox.Show(string.Format(this.TryFindResource("AskForDelete") as string, row.LiftStatusNum + "(" + row.LiftStatusName + ")"), this.TryFindResource("MessageBoxTitle") as string, MessageBoxButton.YesNo, MessageBoxImage.Warning))
            {
                Actions.StatusInfo.DeletestatusResult ret = Actions.StatusInfo.Deletestatus(row.ID);
                switch (ret)
                {
                    case Data.BLL.Actions.StatusInfo.DeletestatusResult.StatusIsNotExist:
                        errmsg.Text = string.Format(this.TryFindResource("IsNotExist") as string, this.TryFindResource("StatusInfo") as string);
                        break;
                    case Data.BLL.Actions.StatusInfo.DeletestatusResult.OK:
                        errmsg.Text = this.TryFindResource("successfuldelete") as string;
                        this.statusList.ItemsSource = Data.DAL.LiftStatus.List("");
                        statusList.Items.Refresh();
                        statusNum.Text = "";
                        statusName.Text = "";
                        statusType.SelectedIndex = 0;
                        break;
                }
            }
        }
    }
}